<template>
  <div class="box">
    <Card>
      <span slot="title">添加账号</span>
      <div slot="title1">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="原密码" prop="pwd1">
            <el-input
              status-icon
              type="text"
              v-model="ruleForm.pwd1"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="pwd2">
            <el-input
              status-icon
              type="password"
              v-model="ruleForm.pwd2"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item label="确认新密码" prop="pwd3">
            <el-input
              status-icon
              type="password"
              v-model="ruleForm.pwd3"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm()">提交</el-button>
            <el-button @click="resetForm()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </Card>
  </div>
</template>

<script>
//引入新老密码的接口
import { apixt_js } from "@/utils/api.js";
//修改密码
import { apijc_js } from "@/utils/api.js";
import local from "@/utils/sess.js";
console.log(Add.pass, Add.pwd);
import Card from "@/components/Card.vue";
import Add from "@/utils/ruler.js";
export default {
  components: {
    Card,
    Add,
  },
  data() {
    let propwd1 = async (rulers, value, callback) => {
      //不能为空
      let { msg, code } = await apixt_js({ oldPwd: value });
      if (!value) {
        callback(new Error("密码不能为空"));
        //检测旧密码是否正确
      } else if (code == "11") {
        callback(new Error (msg));
      }
    };
    //不能和原来的密码一样  不能为空 复合正则验证
    let propwd2 = (rulers, value, callback) => {
      if (!value) {
        callback(new Error("密码不能为空"));
      } else if (!Add.pwd.test(value)) {
        callback(
          new Error(
            "最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符"
          )
        );
      } else if (this.ruleForm.pwd1 == this.ruleForm.pwd2) {
        callback(new Error("新密码和旧密码不能一样"));
      } else {
        callback();
      }
    };
    //密码强度正则，最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符
    let propwd3 = (rulers, value, callback) => {
      if (!value) {
        callback(new Error("密码不能为空"));
      } else if (!Add.pwd.test(value)) {
        callback(
          new Error(
            "最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符"
          )
        );
      } else if (this.ruleForm.pwd2 !== this.ruleForm.pwd3) {
        callback(new Error("两次密码不一样"));
      } else {
        callback();
      }
    };
    //引入组件
    return {
      //表单之王双向绑定
      ruleForm: {
        pwd1: "",
        pwd2: "",
        pwd3: "",
      },
      //验证规则
      rules: {
        //vlidator  自定义正则规则验证
        //账户的验证
        //首先验证原来的密码
        pwd1: { validator: propwd1, trigger: "blur" },
        pwd2: { validator: propwd2, trigger: "blur" },
        pwd3: { validator: propwd3, trigger: "blur" },
      },
    };
  },
  methods: {
    //提交和重置按钮
    submitForm() {
      //提交的时候要同时验证这两个表单的数据
      this.$refs.ruleForm.validate(async (vili) => {
        if (vili) {
          //发送ajax请求
          let { code, msg } = await apijc_js({
            newPwd: this.ruleForm.pwd2,
          });
          if (code == 0) {

            //清空本地退回到登录页
            local.clear();
            this.$router.push("/login")
          }
        } else {
          return;
        }
      });
    },

    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.box {
  padding: 20px;
  box-sizing: border-box;
  background: #ffffff;
}

/deep/.el-input__inner {
  width: 200px;
}
/deep/.el-input {
  width: 200px;
}
</style>